import { PACS, PACS_SETTING } from './src/renderer/src/config/config'
import wasm from 'vite-plugin-wasm'
import topLevelAwait from 'vite-plugin-top-level-await'
import { resolve } from 'path'
import vue from '@vitejs/plugin-vue'

export default {
  resolve: {
    alias: {
      '@cornerstonejs/tools': '@cornerstonejs/tools/dist/umd/index.js',
      '@': resolve('src/renderer/src/')
    }
  },
  server: {
    // 为了解决 SharedArrayBuffer is NOT supported
    headers: {  //为每个请求添加自定义头
      'Cross-Origin-Opener-Policy': 'same-origin',//保护源站免受攻击
      'Cross-Origin-Embedder-Policy': 'require-corp'//保护源站免受侵害
    },
    proxy: {
      '/dicom-web': {
        target: PACS.wadoURIRoot,
        changeOrigin: true,
        rewrite: path => getPrefixReplace(path)
      },
      '/seg-dicom-web': {
        target: PACS.wadoURIRoot,
        changeOrigin: true,
        rewrite: path => path.replace(/^\/seg-dicom-web/, '/dicom-web')
      }
    }
  },
  plugins: [
    wasm(),
    topLevelAwait(),
    vue()
  ],
  worker: {
    // Not needed with vite-plugin-top-level-await >= 1.3.0
    // format: "es",
    plugins: [
      wasm(),
      topLevelAwait()
    ] as any
  }
}

const getPrefixReplace = (path) => {
  let newPath
  if (PACS_SETTING.PACS_SOURCE === 'orthanc') {
    newPath = path
  } else {
    newPath = path.replace(/^\/dicom-web/, '')
  }
  return newPath
}
